home *** CD-ROM | disk | FTP | other *** search
/ United Public Domain Gold 2 / United Public Domain Gold 2.iso / utilities / pu017.dms / pu017.adf / Gadgets / Example5.c < prev    next >
C/C++ Source or Header  |  1990-01-30  |  9KB  |  308 lines

  1. /* Example5                                                             */
  2. /* This program will open a normal window which is connected to the     */
  3. /* Workbench Screen. The window will use all System Gadgets, and will   */
  4. /* close first when the user has selected the System gadget Close       */
  5. /* window. Inside the window we have put a Boolean gadget with two      */
  6. /* Image structures connected to it. Each time the user clicks on the   */
  7. /* gadget it will change images, lamp on/lamp off.                      */
  8.  
  9.  
  10.  
  11. /* Since the program is using Intuition we need to include this file: */
  12. #include <intuition/intuition.h>
  13.  
  14.  
  15.  
  16. struct IntuitionBase *IntuitionBase;
  17.  
  18.  
  19.  
  20. /* The Image data for the dark lamp: */
  21. /* Remember that Image data must ALWAYS be placed in chip memory: */
  22. USHORT chip lamp_off_data[84]=
  23. {
  24.   0x00FF,0x8000, /* Bitplane ZERO */
  25.   0x0700,0x7000,
  26.   0x18F0,0x0C00,
  27.   0x27E0,0x0200,
  28.   0x4800,0x0100,
  29.   0x9100,0x4080,
  30.   0x90AA,0x8080,
  31.   0x8080,0x8080,
  32.   0x4041,0x0100,
  33.   0x2041,0x0200,
  34.   0x1822,0x0C00,
  35.   0x0622,0x3000,
  36.   0x01FF,0xC000,
  37.   0x0150,0x2000,
  38.   0x0205,0x4000,
  39.   0x0150,0x2000,
  40.   0x0205,0x4000,
  41.   0x0150,0x2000,
  42.   0x0205,0x4000,
  43.   0x01FF,0x8000,
  44.   0x003C,0x0000,
  45.  
  46.   0x0000,0x0000, /* Bitplane ONE */
  47.   0x00FF,0x8000,
  48.   0x070F,0xF000,
  49.   0x181F,0xFC00,
  50.   0x37FF,0xFE00,
  51.   0x6EFF,0xBF00,
  52.   0x6F55,0x7F00,
  53.   0x7F7F,0x7F00,
  54.   0x3FBE,0xFE00,
  55.   0x1FBE,0xFC00,
  56.   0x07DD,0xF000,
  57.   0x01DD,0xC000,
  58.   0x0000,0x0000,
  59.   0x00AF,0xC000,
  60.   0x01FA,0x8000,
  61.   0x00AF,0xC000,
  62.   0x01FA,0x8000,
  63.   0x00AF,0xC000,
  64.   0x01FA,0x8000,
  65.   0x0000,0x0000,
  66.   0x0000,0x0000
  67. };
  68.  
  69. /* The Image structure for the dark lamp: */
  70. struct Image lamp_off=
  71. {
  72.   0, 0,          /* LeftEdge, TopEdge */
  73.   25, 21,        /* Width, Height */
  74.   2,             /* Depth */
  75.   lamp_off_data, /* ImageData */
  76.   0x03, 0x00,    /* PlanePick, PlaneOnOff */
  77.   NULL           /* NextImage */
  78. };
  79.  
  80.  
  81.  
  82. /* The Image data for the light lamp: */
  83. /* Remember that Image data must ALWAYS be placed in chip memory: */
  84. USHORT chip lamp_on_data[84]=
  85. {
  86.   0x00FF,0x8000, /* Bitplane ZERO */
  87.   0x07FF,0xF000,
  88.   0x1FFF,0xFC00,
  89.   0x3FFF,0xFE00,
  90.   0x7FFF,0xFF00,
  91.   0xFFFF,0xFF80,
  92.   0xFFFF,0xFF80,
  93.   0xFFFF,0xFF80,
  94.   0x7FFF,0xFF00,
  95.   0x3FFF,0xFE00,
  96.   0x1FFF,0xFC00,
  97.   0x07FF,0xF000,
  98.   0x01FF,0xC000,
  99.   0x0150,0x2000,
  100.   0x0205,0x4000,
  101.   0x0150,0x2000,
  102.   0x0205,0x4000,
  103.   0x0150,0x2000,
  104.   0x0205,0x4000,
  105.   0x01FF,0x8000,
  106.   0x003C,0x0000,
  107.   
  108.   0x0000,0x0000, /* Bitplane ONE */
  109.   0x00FF,0x8000,
  110.   0x070F,0xF000,
  111.   0x181F,0xFC00,
  112.   0x37FF,0xFE00,
  113.   0x6CFF,0x9F00,
  114.   0x6E00,0x3F00,
  115.   0x7E7F,0x3F00,
  116.   0x3F3E,0x7E00,
  117.   0x1F3E,0x7C00,
  118.   0x079C,0xF000,
  119.   0x019C,0xC000,
  120.   0x0000,0x0000,
  121.   0x00AF,0xC000,
  122.   0x01FA,0x8000,
  123.   0x00AF,0xC000,
  124.   0x01FA,0x8000,
  125.   0x00AF,0xC000,
  126.   0x01FA,0x8000,
  127.   0x0000,0x0000,
  128.   0x0000,0x0000
  129. };
  130.  
  131. /* The Image structure for the light lamp: */
  132. struct Image lamp_on=
  133. {
  134.   0, 0,         /* LeftEdge, TopEdge */
  135.   25, 21,       /* Width, Height */
  136.   2,            /* Depth */
  137.   lamp_on_data, /* ImageData */
  138.   0x03, 0x00,   /* PlanePick, PlaneOnOff */
  139.   NULL          /* NextImage */
  140. };
  141.  
  142.  
  143.  
  144. struct Gadget my_gadget=
  145. {
  146.   NULL,          /* NextGadget, no more gadgets in the list. */
  147.   40,            /* LeftEdge, 40 pixels out. */
  148.   20,            /* TopEdge, 20 lines down. */
  149.   25,            /* Width, 25 pixels wide. */
  150.   21,            /* Height, 21 pixels lines heigh. */
  151.   GADGHIMAGE|    /* Flags, display an alternative image when selected. */
  152.   GADGIMAGE,     /* The gadget should be rendered as an Image. */
  153.   GADGIMMEDIATE| /* Activation, our program will recieve a message when */
  154.                  /* the user has selected this gadget. */
  155.   TOGGLESELECT,  /* The on/off state of the gadget is toggled each time. */
  156.   BOOLGADGET,    /* GadgetType, a Boolean gadget. */
  157.   (APTR) &lamp_off, /* GadgetRender, a pointer to our unselected Image. */
  158.                  /* (Since Intuition does not know if this will be a */
  159.                  /* pointer to a Border structure or an Image structure, */
  160.                  /* Intuition expects an APTR (normal memory pointer). */
  161.                  /* We will therefore have to calm down the compiler by */
  162.                  /* doing some "casting". We tell the compiler that */
  163.                  /* the pointer to the Image structure is the same thing */
  164.                  /* as a memory pointer (APTR). */
  165.   (APTR) &lamp_on, /* SelectRender, a pointer to the alternative image. */
  166.   NULL,          /* GadgetText, no text connected to this gadget. */
  167.   NULL,          /* MutualExclude, no mutual exclude. */
  168.   NULL,          /* SpecialInfo, NULL since this is a Boolean gadget. */
  169.                  /* (It is not a Proportional/String or Integer gadget) */
  170.   NULL           /* UserData, no user data connected to the gadget. */
  171. };
  172.  
  173.  
  174.  
  175. /* Declare a pointer to a Window structure: */ 
  176. struct Window *my_window;
  177.  
  178. /* Declare and initialize your NewWindow structure: */
  179. struct NewWindow my_new_window=
  180. {
  181.   50,            /* LeftEdge    x position of the window. */
  182.   25,            /* TopEdge     y positio of the window. */
  183.   200,           /* Width       200 pixels wide. */
  184.   100,           /* Height      100 lines high. */
  185.   0,             /* DetailPen   Text should be drawn with colour reg. 0 */
  186.   1,             /* BlockPen    Blocks should be drawn with colour reg. 1 */
  187.   CLOSEWINDOW|   /* IDCMPFlags  The window will give us a message if the */
  188.                  /*             user has selected the Close window gad, */
  189.   GADGETDOWN,    /*             or a gadget has been pressed on. */
  190.   SMART_REFRESH| /* Flags       Intuition should refresh the window. */
  191.   WINDOWCLOSE|   /*             Close Gadget. */
  192.   WINDOWDRAG|    /*             Drag gadget. */
  193.   WINDOWDEPTH|   /*             Depth arrange Gadgets. */
  194.   WINDOWSIZING|  /*             Sizing Gadget. */
  195.   ACTIVATE,      /*             The window should be Active when opened. */
  196.   &my_gadget,    /* FirstGadget A pointer to my_gadget structure. */
  197.   NULL,          /* CheckMark   Use Intuition's default CheckMark. */
  198.   "ENLIGHTEN ME",/* Title       Title of the window. */
  199.   NULL,          /* Screen      Connected to the Workbench Screen. */
  200.   NULL,          /* BitMap      No Custom BitMap. */
  201.   140,           /* MinWidth    We will not allow the window to become */
  202.   50,            /* MinHeight   smaller than 140 x 50, and not bigger */
  203.   300,           /* MaxWidth    than 300 x 200. */
  204.   200,           /* MaxHeight */
  205.   WBENCHSCREEN   /* Type        Connected to the Workbench Screen. */
  206. };
  207.  
  208.  
  209.  
  210. main()
  211. {
  212.   /* Boolean variable used for the while loop: */
  213.   BOOL close_me;
  214.  
  215.   /* Declare a variable in which we will store the IDCMP flag: */
  216.   ULONG class;
  217.   
  218.   /* Declare a pointer to an IntuiMessage structure: */
  219.   struct IntuiMessage *my_message;
  220.  
  221.  
  222.  
  223.   /* Before we can use Intuition we need to open the Intuition Library: */
  224.   IntuitionBase = (struct IntuitionBase *)
  225.     OpenLibrary( "intuition.library", 0 );
  226.   
  227.   if( IntuitionBase == NULL )
  228.     exit(); /* Could NOT open the Intuition Library! */
  229.  
  230.  
  231.  
  232.   /* We will now try to open the window: */
  233.   my_window = (struct Window *) OpenWindow( &my_new_window );
  234.   
  235.   /* Have we opened the window succesfully? */
  236.   if(my_window == NULL)
  237.   {
  238.     /* Could NOT open the Window! */
  239.     
  240.     /* Close the Intuition Library since we have opened it: */
  241.     CloseLibrary( IntuitionBase );
  242.  
  243.     exit();  
  244.   }
  245.  
  246.  
  247.  
  248.   /* We have opened the window, and everything seems to be OK. */
  249.  
  250.  
  251.  
  252.   close_me = FALSE;
  253.  
  254.   /* Stay in the while loop until the user has selected the Close window */
  255.   /* gadget: */
  256.   while( close_me == FALSE )
  257.   {
  258.     /* Wait until we have recieved a message: */
  259.     Wait( 1 << my_window->UserPort->mp_SigBit );
  260.  
  261.     /* Collect the message: */
  262.     my_message = (struct IntuiMessage *) GetMsg( my_window->UserPort );
  263.  
  264.     /* Have we collected the message sucessfully? */
  265.     if(my_message)
  266.     {
  267.       /* After we have collected the message we can read it, and save any */
  268.       /* important values which we maybe want to check later: */
  269.       class = my_message->Class;
  270.   
  271.       /* After we have read it we reply as fast as possible: */
  272.       /* REMEMBER! Do never try to read a message after you have replied! */
  273.       /* Some other process has maybe changed it. */
  274.       ReplyMsg( my_message );
  275.   
  276.       /* Check which IDCMP flag was sent: */
  277.       switch( class )
  278.       {
  279.         case CLOSEWINDOW:  /* The user selected the Close window gadget! */
  280.                close_me=TRUE;
  281.                break;
  282.                
  283.         case GADGETDOWN:   /* The user has pressed on the Boolean gadget. */
  284.                /* Is the lamp on? */
  285.                /* We check if the SELECTED bit is set: */
  286.                if(my_gadget.Flags & SELECTED)
  287.                  printf("Lamp: ON\n");
  288.                else
  289.                                printf("Lamp: OFF\n");
  290.                                  
  291.                break;
  292.       }
  293.     }
  294.   }  
  295.  
  296.  
  297.  
  298.   /* We should always close the windows we have opened before we leave: */
  299.   CloseWindow( my_window );
  300.  
  301.  
  302.  
  303.   /* Close the Intuition Library since we have opened it: */
  304.   CloseLibrary( IntuitionBase );
  305.   
  306.   /* THE END */
  307. }
  308.